<template>
  <div>
    <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item label="模版名称" prop="tplName">
        <a-input v-model="form.tplName" placeholder="请输入模版名称" />
      </a-form-model-item>
      <a-form-model-item label="模版编码" prop="tplKey">
        <a-input v-model="form.tplKey" placeholder="请输入模版编码" />
      </a-form-model-item>
      <a-form-model-item label="排序" prop="sort">
        <a-input-number v-model="form.sort" placeholder="" />
      </a-form-model-item>
    </a-form-model>
  </div>
</template>
<script>
import { save } from '@/api/formTemplate'
export default {
  name: 'TemplateForm',
  props: {
    value: Object
  },
  data() {
    return {
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      form: Object.assign({
        id: '',
        tplName: '',
        tplKey: '',
        sort: 10,
        status: 0
      }, this.value),
      rules: {
        tplName: [{ required: true, message: '请输入模版名称', trigger: 'blur' }],
        tplKey: [{ required: true, message: '请输入模版编码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          save(this.form).then(res => {
            res.success ? this.$message.success('保存成功') : this.$message.error('保存失败')
            this.$emit('done', res.success)
          }).catch(res => {
            this.$emit('done', res.success)
          })
        } else {
          this.$emit('done', false)
          return false
        }
      })
    },
    resetForm() {
      // 重置表单数据
      this.$refs.ruleForm.resetFields()
      this.$utils.merge(this.form, this.$options.data()['form'])
    },
    loadData(data) {
      if (data) {
        this.$utils.merge(this.form, data)
      }
    }
  }
}
</script>
